package com.xs.anxingo.server.log;

import com.xs.anxingo.server.logbak.LogBackThreadLocal;
import com.xs.anxingo.server.util.JsonUtil;
import lombok.Getter;
import lombok.Setter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.Serializable;
import java.util.Date;

/**
 * 业务监控Monitor日志
 */
public class BizMonitorLogUtil {

    private static final String BIZ_LOG = "BIZ_LOG";

    private static final Logger monitorlog = LoggerFactory.getLogger(BIZ_LOG);

    /**
     * 打印业务
     *
     * @param costTime
     * @param logType
     * @param respCode
     * @param resultCode
     * @param className
     * @param methodName
     * @param platform
     */
    public static void logBizInfo(Long costTime, Boolean runSuccess, String logType, String respCode, String resultCode, String className, String methodName, String platform) {
        BizMonitorLogBean bizMonitorLogBean = new BizMonitorLogBean(costTime, runSuccess, logType, respCode, resultCode, className, methodName, platform);
        monitorlog.info("{}", JsonUtil.toJson(bizMonitorLogBean));
    }


    /**
     * 业务日志实体
     */
    @Getter
    @Setter
    static class BizMonitorLogBean implements Serializable {

        //当前线程id
        private String threadId = LogBackThreadLocal.getCurrentContext();
        //当前时间
        private Date time = new Date();
        //耗时(毫秒)
        private long costTime;
        //当前监控种类 REST/API/REMOTE
        private String logType;
        //是否执行成功
        private Boolean runSuccess;
        //当前请求响应code
        private String respCode;
        //当前请求业务结果code
        private String resultCode;
        //当前请求类
        private String className;
        //当前请求方法
        private String methodName;
        //当前请求来源 PC/H5/IOS/ADD
        private String platform;

        BizMonitorLogBean(Long costTime, Boolean runSuccess, String logType, String respCode, String resultCode, String className, String methodName, String platform) {
            this.costTime = costTime;
            this.runSuccess = runSuccess;
            this.logType = logType;
            this.respCode = respCode;
            this.resultCode = resultCode;
            this.className = className;
            this.methodName = methodName;
            this.platform = platform;
        }

    }
}
